package cn.lei.uicontrols;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

public class AtyUsingImageSwitcher extends AppCompatActivity {

    private ImageSwitcher mImageSwitcher;
    private boolean showImage1 = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aty_using_image_switcher);

        mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
        mImageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                return new ImageView(AtyUsingImageSwitcher.this);
            }
        });
        mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(AtyUsingImageSwitcher.this, android.R.anim.fade_in));
        mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(AtyUsingImageSwitcher.this, android.R.anim.fade_out));

        mImageSwitcher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showImage1 = !showImage1;
                showCurrentImage();
            }
        });
        showCurrentImage();
    }

    private void showCurrentImage() {
        if (showImage1) {
            mImageSwitcher.setImageResource(R.drawable.image2);
        } else {
            mImageSwitcher.setImageResource(R.drawable.image1);
        }
    }
}
